//
//  DYNavigationController.m
//  dyss_v2
//
//  Created by LXF on 16/1/26.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "DYNavigationController.h"
#import "SkinManager.h"

@interface DYNavigationController ()<UIGestureRecognizerDelegate,ReloadSkinResourceProtocal>

@end

@implementation DYNavigationController

-(void)dealloc{
    [self xx_deRegister];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self xx_register];
    
    [self prepareStyle];
    self.enabledInteractivePopGesture = YES;
    self.enabledInteractivePopGesture_FULL = YES;
    
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
#pragma clang diagnostic pop
    
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

// 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    if (self.enabledInteractivePopGesture_FULL == NO) {
        return NO;
    }
    
    if (self.enabledInteractivePopGesture == NO) {
        CGPoint touch = [gestureRecognizer locationInView:self.view];
        if (touch.x > 100) {
            return NO;
        }else{
            return YES;
        }
    }
    
    __block BOOL needReturn = NO;
    [self.popGestureIgnoreViews xx_enumerateObjectsUsingBlock:^(UIView *obj, BOOL *stop) {
        if(obj == nil){
            return ;
        }
        CGPoint touch = [gestureRecognizer locationInView:obj];
//        printW(@"=====> %@",NSStringFromCGPoint(touch));
        if (CGRectContainsPoint(obj.bounds,touch)){
            needReturn = YES;
            *stop = YES;
        }
    }];
    if (needReturn) {
        return NO;
    }
    
    return self.enabledInteractivePopGesture;
}

-(void)setEnabledInteractivePopGesture:(BOOL)enabledInteractivePopGesture{
    _enabledInteractivePopGesture = enabledInteractivePopGesture;
    
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count) { // 不是根控制器才需要
        UIImage *image = [UIImage skin:@"navbar_back"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(clickBackButton)];
        viewController.navigationItem.leftBarButtonItem = backItem;
        
        viewController.hidesBottomBarWhenPushed = YES;
        
    }
    
    [super pushViewController:viewController animated:animated];
}
-(void)clickBackButton{
    [self popViewControllerAnimated:YES];
}

-(void)execReloadResource{
    [self prepareStyle];
}

- (void)prepareStyle{
    //set NavigationBar 背景颜色&title 颜色
    
    [self.navigationBar setBarTintColor:[UIColor skin:@"font_color"]];
    
    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];// UITextAttributeTextColor
}

-(void)popGestureIgnoreViewsAdd:(UIView *)view{
    [self.popGestureIgnoreViews xx_addObject:view];
}

-(NSMutableSet<UIView *> *)popGestureIgnoreViews{
    if (_popGestureIgnoreViews == nil) {
        NSMutableSet<UIView *> *a = [[NSMutableSet<UIView *> alloc] init];
        _popGestureIgnoreViews = a;
    }
    return _popGestureIgnoreViews;
}


@end
